﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Facebook;
using FaceExcel.EventArg;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Interop.Excel;
using Worksheet = Microsoft.Office.Interop.Excel.Worksheet;

namespace FaceExcel
{
    public partial class MainRibbon
    {
        private void MainRibbon_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private bool isLogin = false;
        private const string LoginButtonString = "Login!";
        private const string RefreshButtonString = "Refresh!";
        private string FBTokenString = null;
        private int currentCounter = 0;

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            if (!isLogin)
            {
                LoginControl LG = new LoginControl();
                LG.AuthFinish += new EventHandler<EventArg.EventFacebookOAuthResult>(LG_AuthFinish);
                if (LG.ShowDialog() == DialogResult.OK)
                {
                    
                }
            } else
            {
                //refresh the UI
                FecthHomeFeed();
            }
        }

        void LG_AuthFinish(object sender, EventArg.EventFacebookOAuthResult e)
        {
            isLogin = true;
            LoginButton.Label = RefreshButtonString;
            FBTokenString = e.AuthResult.AccessToken;
            FecthHomeFeed();
        }

        private string[] title = {"From", "To", "Message", "Type", "Likes", "Comments"};
        private string[] titleLocation = {"B","C","D","E","F","G"};
        Microsoft.Office.Tools.Excel.NamedRange clickRange;
        private void FecthHomeFeed()
        {
            var fb = new FacebookClient(FBTokenString);
            dynamic result = fb.Get("me/home");
            Worksheet currentSheet = Globals.ThisAddIn.Application.ActiveSheet as Worksheet;
            int counter = 0;
            Microsoft.Office.Tools.Excel.Worksheet  bg = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveSheet);
            var ll = bg.Controls;
            clickRange = ll.AddNamedRange(currentSheet.get_Range("A1"), "hore");
            clickRange.BeforeDoubleClick += new DocEvents_BeforeDoubleClickEventHandler(clickRange_BeforeDoubleClick);
            currentSheet.BeforeDoubleClick += new DocEvents_BeforeDoubleClickEventHandler(currentSheet_BeforeDoubleClick);
           
            
            //write the title
            foreach (var tit in title)
            {
                Range titleRange = currentSheet.get_Range(titleLocation[counter] + 1.ToString());
                
                titleRange.Value2 = tit;
                titleRange.Font.Bold =true;
                counter++;
            }

            counter = 2;

            //clear the sheet
            if (currentCounter>2)
            {
                Range rangeToClear = currentSheet.get_Range("C2:D" + currentCounter.ToString());
                rangeToClear.Clear();
            }




            //fill the sheet with fb data
            foreach (dynamic val in result.data)
            {
                if (val.message != null)
                {
                    //show the author
                    Range authorHolder = currentSheet.get_Range("B" + counter.ToString());
                    authorHolder.Value2 = val.from.name;
                    authorHolder.Columns.AutoFit();

                    //show to
                    if (val.to != null)
                    {
                        Range toHolder = currentSheet.get_Range("C" + counter.ToString());
                        toHolder.Value2 = val.to.data[0].name;
                        toHolder.Columns.AutoFit();
                    }

                    // show the message
                    Range messageHolder= currentSheet.get_Range("D"+counter.ToString());
                    messageHolder.Value2 = val.message;
                    messageHolder.Columns.AutoFit();
                    
                    //show the type 
                    Range typeHolder = currentSheet.get_Range("E" + counter.ToString());
                    typeHolder.Value2 = val.type;

                    //show how many comments
                    if (val.comments != null)
                    {
                        Range likesHolder = currentSheet.get_Range("F" + counter.ToString());
                        likesHolder.Value2 = val.comments.count;
                    }

                    //show how many likes.
                    if (val.likes != null)
                    {
                        Range commentHolder = currentSheet.get_Range("G" + counter.ToString());
                        commentHolder.Value2 = val.likes.count;
                    }
                    counter++;

                }
            }
            currentCounter = counter;
        }

        void clickRange_BeforeDoubleClick(Range Target, ref bool Cancel)
        {
            MessageBox.Show(clickRange.Value2.ToString());
        }

        void currentSheet_BeforeDoubleClick(Range Target, ref bool Cancel)
        {

        }
    }
}
